<!-- 创建宿舍楼 -->
<template>
  <div class="addDormitory-container">
    <TitleBar title="创建宿舍楼"></TitleBar>
    <BackgroundBox>
      <el-form
        :model="ruleForm"
        :inline="true"
        class="form"
        style="min-width: 600px"
        ref="ruleFormRef"
        :rules="rules"
      >
        <div class="item input">
          <el-form-item label="宿舍楼名" class="from-item" prop="dormitoryName">
            <el-input style="width: 100px" v-model="ruleForm.dormitoryName" />
          </el-form-item>
          <el-form-item
            label="楼层数"
            class="from-item"
            prop="floorNum"
            required
          >
            <el-input-number
              style="width: 100px"
              :min="0"
              v-model="ruleForm.floorNum"
            />
          </el-form-item>
          <el-form-item
            label="每层房间数"
            class="from-item"
            prop="roomNum"
            required
          >
            <el-input-number
              :min="0"
              style="width: 100px"
              v-model="ruleForm.roomNum"
            />
          </el-form-item>
          <el-form-item
            label="每间人数"
            class="from-item"
            prop="peopleNum"
            required
          >
            <el-input-number
              :min="0"
              style="width: 100px"
              v-model="ruleForm.peopleNum"
              @change="headleGetRoomRate(ruleForm.peopleNum)"
            />
          </el-form-item>
          <el-form-item label="宿舍费/元" prop="price" class="from-item">
            <el-input
              :disabled="!isruleFormExitsRef"
              style="width: 90px"
              v-model.number="ruleForm.price"
            />
          </el-form-item>
        </div>
        <div class="item but">
          <el-form-item class="from-item">
            <el-button type="primary" @click="submitForm(ruleFormRef)"
              >创建</el-button
            >
            <el-button type="danger" @click="resetForm(ruleFormRef)"
              >清空</el-button
            >
          </el-form-item>
        </div>
      </el-form>
      <p class="err" v-show="isruleFormExitsRef">
        注:没有查询到{{
          ruleForm.peopleNum
        }}人间的宿舍费,请输入宿舍费,本次输入的宿舍费将保存下来以便下次使用
      </p>
    </BackgroundBox>
    <Prompt
      :isPrompt="isPromptRef"
      @closePrompt="isPromptRef = false"
      :AddDormitoryData="{ ...RoomRateRef, ...ruleForm }"
      @submitPrompt="SuccessCreated"
    ></Prompt>
  </div>
</template>
<script setup>
import TitleBar from "@/components/TitleBar/index.vue";
import BackgroundBox from "@/components/BackgroundBox/index.vue";
import { getRoomRate } from "@/api/dormitoryInfo.js";
import Prompt from "./AddPrompt.vue";
import debounce from "@/util/debounce.js";
import { ref, reactive } from "vue";
import { deepClone } from "@/util/copy.js";
const ruleFormRef = ref();
const RoomRateRef = ref(null); // 宿舍费对象
const isruleFormExitsRef = ref(false); // 是否出现提示
const isPromptRef = ref(false); // 是否开启提示框
const ruleForm = reactive({
  dormitoryName: "",
  floorNum: 0,
  roomNum: 0,
  peopleNum: 0,
  price: "",
});
const copyRuleForm = deepClone(ruleForm);
// 输入的值必须大于0
const checkNum = (rule, value, callback) => {
  if (!value) {
    return callback(new Error("必须大于0"));
  }
  setTimeout(() => {
    if (!Number.isInteger(value)) {
      callback(new Error("请输入数字"));
    } else {
      if (value < 0) {
        callback(new Error("必须大于0"));
      } else {
        callback();
      }
    }
  }, 100);
};
// 表单验证
const rules = reactive({
  dormitoryName: [
    { required: true, message: "请输入宿舍楼名称", trigger: "blur" },
    { min: 2, max: 5, message: "2-5个字符", trigger: "blur" },
  ],
  floorNum: [{ validator: checkNum, trigger: "blur" }],
  roomNum: [{ validator: checkNum, trigger: "blur" }],
  peopleNum: [{ validator: checkNum, trigger: "blur" }],
  price: [
    { required: true, message: "请输入价格", trigger: "blur" },
    { validator: checkNum, trigger: "blur" },
  ],
});
// 提交(打开提示提示框)
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid) => {
    if (valid) {

      isPromptRef.value = true;
    } else {
      ElMessage.error("请完成列表输入");
    }
  });
};
// 清空
const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
// 查询宿舍费
const headleGetRoomRate = (peopleNum) => {
  isruleFormExitsRef.value = false;
  ruleForm.price = "";
  debounce(async function () {
    if (!peopleNum) return;
    const { data } = await getRoomRate(peopleNum);
    ruleForm.price = data.price;
    if (!data.isExist) {
      isruleFormExitsRef.value = true;
    }
    RoomRateRef.value = data;

  }, 200);
};
// 成功创建
const SuccessCreated = () => {
  for (const key in ruleForm) {
    ruleForm[key] = copyRuleForm[key];
  }
  isPromptRef.value = false;
  isruleFormExitsRef.value = false;
};
</script>

<style lang="less" scoped>
@import "@/styles/var.less";
.addDormitory-container {
  width: 100%;
  .back-container {
    padding: 0 20px;
    position: relative;
    height: 120px;
  }
  .form {
    height: 100%;
    display: flex;
    align-items: center;
    .input {
      flex: 1 1 auto;
    }
    .but {
      flex: 0 0 auto;
    }
  }
  .from-item {
    margin: 0;
    margin-right: 20px;
    ::v-deep .el-form-item__label {
      color: @text;
      font-weight: 600;
    }
  }
  .err {
    font-size: 0.7em;
    color: @danger;
    position: absolute;
    right: 50%;
    transform: translateX(50%);
    bottom: 5px;
  }
}
</style>